package cn.gene.timeyan.ums.webapi.ums.service.impl;

import cn.gene.timeyan.common.exception.TimeyanServiceException;
import cn.gene.timeyan.pojo.ums.dto.VIPDTO;
import cn.gene.timeyan.pojo.ums.dto.VipUserIdDTO;
import cn.gene.timeyan.pojo.ums.model.VIP;
import cn.gene.timeyan.pojo.ums.vo.VIPVO;
import cn.gene.timeyan.ums.service.IVIPService;
import cn.gene.timeyan.ums.webapi.ums.mapper.VIPMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

import static cn.gene.timeyan.common.restful.ResponseCode.INTERNAL_SERVER_ERROR;

/**
 * VIP    服务实现类*
 *
 * @author LWG
 * @date 2022/7/22 17:31
 * @since 1.0.0@gene.cn
 */
@DubboService
@Service
@Slf4j
public class VIPServiceImpl implements IVIPService {
    /*
    实现vip新增
     */
    @Autowired(required = false)
    private VIPMapper vipMapper;

    @Override
    public void addNewVIP(VIPDTO vipdto) {

        // 创建VIP对象
        VIP vip = new VIP();


        log.trace("业务逻辑层开始，开始处理vip业务");
        log.debug("尝试添加的vip账号信息：" + vipdto);
        // 通过参数获取用户id
        log.trace("判断此账号是否已经开通vip");
        vipdto.setNow(LocalDateTime.now());
        VIPVO queryResult = vipMapper.selectVIPIdByUserId(vipdto);


        // 判断查询结果是否不为null
        if (queryResult != null) {
            // 是：表示用户已经开通vip，提示用户已经开通，是否需要续期
            log.info("此账号用户已经开通，是否需要续期");

            //获取续费状态
            Integer state = vipdto.getState();
            //表示用户选择续费
            if (state == 1) {
                LocalDateTime endTime = queryResult.getEndTime();
                log.info("endTime", endTime);

                //当用户选择续费后，将结束时间延长即可
                queryResult.setEndTime(queryResult.getEndTime().plusDays(30));

                log.info("续费成功");
                vipMapper.updateEndtime(vipdto.getUserId());
//                // 补全VIP对象的属性值
//                BeanUtils.copyProperties(vipdto, vip);
//                // 补全VIP对象的属性值
//                vip.setRechangeNum(20);
//                vip.setTotleTime(20);
//                vip.setHobbyView("2");
//                vip.setTopOpportunity("2");
//                vip.setApologizeNum(20);
//                vip.setConfessionNum(10);

            }else {
                log.info("续费失败");
            }
        } else {
            //set time
            // 日志
            log.trace("此账号未开通，准备向表中写入此次添加的vip用户信息");
            // 创建当前时间对象now > LocalDateTime.now()
            LocalDateTime now = LocalDateTime.now();

            // 补全VIP对象的属性值
            BeanUtils.copyProperties(vipdto, vip);
            // 补全VIP对象的属性值
            vip.setStartTime(now);
            vip.setEndTime(now.plusDays(30));
            vip.setRechangeNum(10);

            vip.setTotleTime(10);
            vip.setHobbyView("1");
            vip.setViewTime(now);
            vip.setNextView(now);
            vip.setTopOpportunity("1");
            vip.setApologizeNum(10);
            vip.setConfessionNum(5);
            vip.setCreateTime(now);
            vip.setUpdateTime(now);


            // 调用vipMapper的方法插入用户数据，获取返回值
            log.debug("即将插入新的vip用户信息：{}", vip);
            int rows = vipMapper.insertVIP(vip);

            // 判断以上返回的结果是否不为1，抛出异常
            if (rows != 1) {
                throw new TimeyanServiceException(INTERNAL_SERVER_ERROR, "添加vip用户失败，服务器忙，请稍后再次尝试！");
            }
        }
    }

    @Override
    public VipUserIdDTO IGetVipByUserIdService(Long userId) {
        return vipMapper.getVipByUserId(userId);

    }

}
